﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CheckHD
{
    class HDSerial
    {
        //Cliente
        const string MY_SERIAL = "B0AC-BE43";
        //const string MY_SERIAL_H = "AC81-C680";
        //Rui Neves
        //const string MY_SERIAL_H = "4A8B-E9B3";
        //const string MY_SERIAL_H = "38C2-68A4";
        //CMG
        const string MY_SERIAL_H = "AC81-C680";
        //Toshiba
        //const string MY_SERIAL_H = "5044-201A";
        public static bool CheckSerial()
        {
            string res = ExecuteCommandSync("vol");
            //const 
            string search = "volume ‚ ";
            int startI = res.IndexOf(search, StringComparison.InvariantCultureIgnoreCase);
            if (startI < 1)
            {
                search = "Number is ";
                startI = res.IndexOf(search, StringComparison.InvariantCultureIgnoreCase);
            }

            if (startI > 0)
            {
                string currentDiskID = res.Substring(startI + search.Length).Trim();
                if (currentDiskID.Equals(MY_SERIAL))
                    return true;
                if (currentDiskID.Equals(MY_SERIAL_H))
                    return true;

            }
            return false;
        }

        public static string ExecuteCommandSync(object command)
        {
            try
            {
                // create the ProcessStartInfo using "cmd" as the program to be run,
                // and "/c " as the parameters.
                // Incidentally, /c tells cmd that we want it to execute the command that follows,
                // and then exit.
                System.Diagnostics.ProcessStartInfo procStartInfo =
                    new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

                // The following commands are needed to redirect the standard output.
                // This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute = false;
                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;
                // Now we create a process, assign its ProcessStartInfo and start it
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
                // Get the output into a string
                string result = proc.StandardOutput.ReadToEnd();
                // Display the command output.
                return result;
            }
            catch (Exception)
            {
                // Log the exception
                return null;
            }
        }
    }
}
